<template>
	<view class="setPadding">
		
		<navigationBar  :titleName="titleName"></navigationBar>
		<u-toast ref="uToast" />
		<navigator v-if="!flag" url="/pagesUser/feedbackRecord" class=" settextRight setFontColor1 setMarginBottom" >
			<text class="setMarginBottom">反馈记录</text>
		</navigator>
		<view class="item setPadding setDisplayCenter">
			<textarea maxlength="100" v-model="content" placeholder="请输入内容…"></textarea>
		</view>
		
		<view class="item setPadding">
			<text class="setFontColor1" style="margin-bottom: 10rpx;display: block;">上传截图或凭证</text>
			<u-upload height="150rpx" width="150rpx"  :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1"
				multiple :maxCount="10"></u-upload>
		</view>

		<view class="button" hover-class="button-hover" @tap="save"><text>提 交</text></view>
	</view>
</template>

<script>
	import iconfont from '@/components/iconfont/iconfont.vue';
	export default {
		components: {
			iconfont
		},
		data() {
			return {
				content: '',
				fileList1: [],
				// 云图片地址,
				imgUrl:[],
				titleName:'举报反馈',
				flag:false,
				over:false
			};
		},
		onShow() {
			this.$initPageTitle(); //初始化页面标题
		},
		onLoad(option) {
			console.log("查看参数option",option)
			if(option.page=="cancellation"){
				this.titleName='注销用户'
				this.flag=true
				this.content="申请账号注销！要求才能招标将我账号下所有内容清空，会员天数清零。承诺造成的后果自己承担！"
			}
		},
		onPullDownRefresh() {},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'https://caineng.com/caineng-api6/consumer/img/uploadZhaobiao', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'files',
						header: {
							'cainengtoken': uni.getStorageSync('accessToken'),
							'Content-type': 'application/x-www-form-urlencoded'
						},
						success: (res) => {
							// console.log('res____________',res)
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
							let group = JSON.parse(res.data)
							// console.log('res____________',res,group)
							if(group.code==0){
								this.imgUrl.push(group.data[0])
							}
						
							
						}
					});
				})
			},
			/*提交*/
			save() {
				console.log("this.imgUrl",this.imgUrl)
				if(this.imgUrl){
					this.imgUrl=this.imgUrl.toString()
					// console.log("this.imgUrl",this.imgUrl)
				}
				// console.log("fileList1",this.fileList1)
				if (this.content == '') {
					this.$alert('请输入您的举报和反馈内容');
					return;
				}
				uni.showLoading({
					title: '提交中…'
				});
				this.$app.request({
					url: this.$baseApp.api.user.feedback,
					data: {
						content: this.content,
						imgPath:this.imgUrl
					},
					method: 'POST',
					dataType: 'json',
					success: res => {
						console.log(res);
						if (res.code == 200) {
								uni.hideLoading();
							if(this.flag){
								this.$refs.uToast.show({
									message: '后台正在进行人工审核,处理时间为2个工作日!',
									type: 'success'
								})
								// 清除缓存 跳转至首页
								setTimeout(()=>{
									uni.clearStorage();
									uni.switchTab({
										url:'/pages/index/index'
									})
								},2000)
							}else{
							
								this.$alert('提交成功');
								this.content = '';
								this.imgUrl=[]
								this.fileList1=[]
							}
						} else {
							this.$alert(res.msg);
						}
						// 如果该操作为注销用户
						
					},
					complete: res => {
						uni.stopPullDownRefresh();
					}
				});
			}
		}
	};
</script>

<style scoped lang="scss">
	.item {
		// padding: 50upx 40upx 20rpx;
		background: #fff;
		margin-bottom: 20rpx;
		textarea {
			height: 350rpx;
			font-size: 32rpx;
		}
	}



	.button {
		margin-top: 80rpx;
	}
</style>
